package com.qfmrk.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfmrk.entity.User;
import com.qfmrk.service.UserService;
import com.qfmrk.util.R;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * --- 永无霸哥 ---
 *
 * @author M.rk
 * @date 2024/11/13 15:26
 * @desc
 */
@RestController
@CrossOrigin
//@Api("用户接口")
public class UserController {
    private Logger logger = LogManager.getLogger(UserController.class);
    @Autowired
    private UserService userService;

    @Value("${com.qf.salt}")
    private String salt;

    /**
     * 演示,获取配置文件中的值
     */
    @GetMapping("/test")
//    @ApiOperation("测试从配置文件获取数据")
    public R testSalt() {

        // 获取配置文件中的数据
        //System.out.println("salt6666 = " +salt );
        logger.debug("debug-salt6666=" + salt);
        logger.debug("debug-salt6666={}", salt);
        logger.info("info-salt6666={}", salt);
        logger.warn("warn-salt6666={}", salt);
        logger.error("error-salt6666={}", salt);

        return R.ok(salt);
    }


    @PostMapping("/user/login")
    public R userLogin(@RequestBody User user) {
        User login = userService.userLogin(user);
        if (login != null) {
            return R.ok(login);
        }

        return R.fail();
    }

    @GetMapping("/user/list")
    public R findUserList(@RequestParam HashMap<String, Object> map) {
        logger.debug("在这里是map-》{}" + map);
        String pageSize = (String) map.get("pageSize");
        String PageNum = (String) map.get("PageNum");

        // 使用分页助手开始分页
        PageHelper.startPage(Integer.parseInt(PageNum), Integer.parseInt(pageSize));

        List<User> userList = userService.findUserList(map);
        System.out.println("userList = " + userList);

        //创建页面信息类
        PageInfo<User> pageInfo = new PageInfo<>(userList);

        return R.ok(pageInfo);
    }


    @GetMapping("/user/handleDelete")
    public R handleDelete(int id) {
        int i = userService.handleDelete(id);

        return R.ok(i);
    }


    @GetMapping("/user/delBatch")
    public R delBatch(@RequestParam ArrayList<Integer> ids) {

        logger.debug("数组在这里-》{}" + ids);

        int i = userService.delBatch(ids);

        if (i > 0) {
            return R.ok();

        }
        return R.fail();
    }

    @PostMapping("/user/appendUser")
    public R appendUser(@RequestBody User user) {

        logger.debug("注意就看这里----》"+user);

        int i = userService.appendUser(user);

        if(i > 0){
            return R.ok();
        }
        return R.fail();
    }


    @PostMapping("/user/updateByUser")
    public R updateByUser(@RequestBody User user) {

        logger.debug("注意就看这里----》"+user);

        int i = userService.updateByUser(user);

        if(i > 0){
            return R.ok();
        }
        return R.fail();
    }

}
